package com.dragon.app.controller.finance;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.app.config.annotation.AddPage;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.finance.IReimbursementService;
import com.dragon.repository.domain.dto.ReimbursementDTO;
import com.dragon.repository.domain.vo.ReimbursementVO;
import com.dragon.repository.entity.Reimbursement;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 报销申请表 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-07-03
 */
@RestController
@RequestMapping("reimbursement")
@Api(tags = "财务报销管理")
public class ReimbursementController {

    @Autowired
    IReimbursementService service;

    @GetMapping("list")
    @ApiOperation("获取报销列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, dataType = "int"),
            @ApiImplicitParam(name = "search", value = "搜索参数", required = false, dataType = "String"),
            @ApiImplicitParam(name = "status", value = "状态", required = false, dataType = "int"),
            @ApiImplicitParam(name = "paymentStatus", value = "收款状态", required = false, dataType = "int"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = false, type = "string"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, type = "string")
    })
    public ResponseData<List<ReimbursementDTO>> reimburseList(@RequestParam Integer page,
                                                              @RequestParam Integer limit,
                                                              @RequestParam(required = false) String search,
                                                              @RequestParam(required = false) Integer status,
                                                              @RequestParam(required = false) Integer paymentStatus,
                                                              @RequestParam(required = false) String startTime,
                                                              @RequestParam(required = false) String endTime){
        Page<Reimbursement> rePage = service.reimburseList(page, limit, search, status , paymentStatus, startTime, endTime);
        return ResponseData.success(ReimbursementDTO.convertList(rePage.getRecords()), rePage.getTotal());
    }

    @PostMapping("add")
    @ApiOperation("新增报销")
    @AddPage
    public ResponseData addReimburse(@RequestBody @Validated(Insert.class)ReimbursementVO vo) {
        return ResponseData.success(service.addReimburse(ReimbursementVO.convert(vo)));
    }

    @PostMapping("update")
    @ApiOperation("修改报销")
    public ResponseData updateReimburse(@RequestBody @Validated(Update.class)ReimbursementVO vo) {
        service.updateReimburse(ReimbursementVO.convert(vo));
        return ResponseData.success();
    }


    @PostMapping("payment")
    @ApiOperation("报销付款操作")
    public ResponseData reimbursePayment(@RequestBody @Validated(Update.class)ReimbursementVO vo) {
        service.reimbursePayment(ReimbursementVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("delete")
    @ApiOperation("删除报销")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "报销id", required = true, type = "int"),
    })
    public ResponseData deleteReimburse(@RequestParam Integer id){
        service.deleteReimburse(id);
        return ResponseData.success();
    }

    @PostMapping("listByReimburseId")
    @ApiOperation("根据报销id获取详情")
    public ResponseData listByReimburseId( @RequestParam
                                           @ApiParam(name = "id",value = "报销id", required = true, type = "int") Integer id){
        return ResponseData.success(service.selectInfoById(id));
    }

}
